<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜单列表</title>
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'treetable'], function () {
        const $ = layui.jquery;
        const table = layui.table;
        const treetable = layui.treetable;
        const form = layui.form;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            url: '/menu/queryAllMenu',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'title', minWidth: 100, title: '菜单名称'},
                {
                    field: 'icon', title: '菜单图标', templet: function (d) {
                        return '<span><i class="' + d.icon + '"></i></span>';
                    }, align: 'center', minWidth: 40
                },
                {field: 'href', title: '菜单url', minWidth: 120, align: 'center'},
                {field: 'sort', minWidth: 40, align: 'center', title: '排序号'},
                {
                    field: 'type', minWidth: 40, align: 'center', templet: function (d) {
                        if (d.type === 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    templet: '<p>{{layui.util.toDateString(d.createTime)}}</p>',
                    align: 'center',
                    minWidth: 150
                },
                {
                    field: 'updateTime',
                    title: '更新时间',
                    templet: '<p>{{layui.util.toDateString(d.updateTime)}}</p>',
                    align: 'center',
                    minWidth: 150
                },
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //删除功能
        function deleteBookById(id) {
            $.ajax({
                url: "/menu/deleteMenu",
                type: "DELETE",
                data: {id: id},
                success: function (data) {
                    if (data.code === 1) {
                        layer.msg('删除成功', {
                            icon: 6,
                            time: 1500
                        }, function () {
                            parent.window.location.reload();
                        });
                    } else {
                        layer.msg(data.msg);
                    }
                }
            });
        };

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            const data = obj.data;
            const layEvent = obj.event;
            if (layEvent === 'del') {
                deleteBookById(data.id);
            }
        });
    });
</script>
</body>
</html>